package main

import (
	"log"
	"mgs/config"
	"mgs/global"
	"mgs/handle"
	"mgs/nacos"
	"mgs/oss"
	"mgs/toolkit"
	"mgs/toolkit/phone"
	"net/http"
)

func main() {
	// 配置初始化
	cfg := config.Init()
	global.CONFIG = cfg

	// 注册服务
	nacos.Register(cfg)

	// 初始化 minio
	oss.InitMinio(cfg)

	// 初始化手机归属地
	phone.Init()

	// API 接口
	var apiPrefix = cfg.System.ApiPrefix
	http.HandleFunc(apiPrefix+"/test/file2pdf", handle.TestFile2pdf)
	http.HandleFunc(apiPrefix+"/office/file2pdf", handle.File2pdf)
	http.HandleFunc(apiPrefix+"/office/key2pdf", handle.Key2pdf)
	http.HandleFunc(apiPrefix+"/video/probe", handle.VideoProbe)
	http.HandleFunc(apiPrefix+"/search/phone", handle.PhoneSearch)
	http.HandleFunc(apiPrefix+"/search/ip", handle.IpSearch)

	// 静态文件服务配置
	var sfs = cfg.Sfs
	if len(sfs.Url) > 0 && len(sfs.Path) > 0 {
		fs := http.Dir(sfs.Path)
		var fsh http.Handler
		if sfs.Preview {
			fsh = http.FileServer(fs)
		} else {
			fsh = http.FileServer(handle.NoListFileSystem{Base: fs})
		}
		http.Handle(sfs.Url, http.StripPrefix(sfs.Url, fsh))
	}

	// 服务端口
	var port = cfg.System.Port
	log.Println("Server is listening on port " + port)

	// 启动服务
	go func() {
		err := http.ListenAndServe(":"+port, nil)
		if err != nil {
			log.Fatal("ListenAndServe: ", err)
		}
	}()

	// 初始化任务调度器
	toolkit.InitScheduler()
}
